home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1997 April / macformat-049.iso / mac / Shareware Plus / Sound / DSM Player 1.0 / DSM API 1.04 / main.cp < prev    next >
Encoding:
Text File  |  1996-12-29  |  3.3 KB  |  173 lines  |  [TEXT/CWIE]

  1. #include <profiler.h>
  2. #include "dsm.h"
  3.  
  4. #ifndef __POWERPC__
  5. int errno = 0;
  6. #endif
  7.  
  8. static Boolean CheckForCmdPeriod (void)
  9. {
  10.     KeyMap    keys;
  11.  
  12.     GetKeys (keys);
  13.     if(keys [1] & 0x00800000)           // command
  14.     {
  15.         if(keys[1] & 0x00008000)        // period
  16.         {
  17.             FlushEvents (mDownMask + mUpMask, 0);
  18.             return TRUE;
  19.         }
  20.     }
  21.     return FALSE;
  22. }
  23.  
  24. static void InitToolBox()
  25. {
  26.     EventRecord    event;
  27.     short        count;
  28.     
  29.     InitGraf((Ptr) &qd.thePort);
  30.     InitFonts();
  31.     InitWindows();
  32.     InitMenus();
  33.     TEInit();
  34.     InitDialogs(0L);
  35.     InitCursor();
  36.     SetEventMask(everyEvent);
  37.  
  38.     for (count = 1; count <= 3; count++) {
  39.         MoreMasters();
  40.         EventAvail(everyEvent, &event);
  41.     }
  42.     FlushEvents(everyEvent, 0);
  43. }
  44.  
  45. void main(void)
  46. {
  47.     OSErr         iErr;
  48.     word        i=1, track, polyphony=2;
  49.     EventRecord theEvent;
  50.  
  51.     InitToolBox();
  52.  
  53.     //    Create new synthesiser: 
  54.  
  55.     //        Mixing Freq: 44 kHz
  56.     
  57.     //        Stereo:         True            Lerping:     True
  58.     //        16-Bit:         True            Looping:     False
  59.     //        Surround:     True
  60.  
  61. #ifdef __POWERPC__
  62.     Synthesiser    *dsm = new Synthesiser(PM_SURROUND + PM_STEREO + PM_INTERP + PM_16BIT, mix44kHz, true);
  63. #else
  64.     Synthesiser    *dsm = new Synthesiser(PM_SURROUND + PM_STEREO + PM_INTERP, mix22kHz, true);
  65. #endif
  66.  
  67.     SFReply        reply;
  68.     SFTypeList     typeList = { '????' };
  69.     Point        where = { -1, -1 };
  70.  
  71.     SFGetFile(where, "\p", NULL, -1, typeList, NULL, &reply);
  72.     if (!reply.good) {
  73.         ExitToShell();
  74.     }
  75.     
  76.     FileMod *song = new FileMod(reply);
  77.     
  78.     //    To load a mod from resource, uncomment next line:
  79.     //    ResMod *song = new ResMod('song', 128);    
  80.  
  81.  
  82.     //    Always check if mod has loaded, or you might crash.
  83.     
  84.     if (song->getError()) 
  85.     {
  86.         delete dsm;
  87.         delete song;
  88.         
  89.         return;
  90.     }
  91.     
  92.     //    You're only allowed to attach one song to synthesiser at a time.
  93.     //    AttachMod() must not be called while synthesiser is active
  94.     
  95.     dsm->AttachMod(*song);
  96.     
  97.     //    Let the music play!
  98.     
  99.     dsm->PlayStart();
  100.  
  101.     while (!CheckForCmdPeriod() && !dsm->IsDone())
  102.     {
  103.         if (WaitNextEvent(everyEvent, &theEvent, 0, NULL))
  104.             switch (theEvent.what) {
  105.                 case keyDown:
  106.                 case autoKey:
  107.                 {
  108.                     switch (theEvent.message & charCodeMask)
  109.                     {
  110.                         case '\r':
  111.                             polyphony = (polyphony+1) % 3;
  112.                             switch (polyphony)
  113.                             {
  114.                                 case 0:
  115.                                     dsm->SetMixMode(dsm->GetMixMode() 
  116.                                         & ~PM_STEREO & ~PM_SURROUND);
  117.                                     break;
  118.                                 case 1:
  119.                                     dsm->SetMixMode(dsm->GetMixMode() 
  120.                                         | PM_STEREO & ~PM_SURROUND);
  121.                                     break;
  122.                                 case 2:
  123.                                     dsm->SetMixMode(dsm->GetMixMode() 
  124.                                         | PM_STEREO | PM_SURROUND);
  125.                                     break;
  126.                             }
  127.                             break;
  128.                         case '-':
  129.                         {
  130.                             uword mastervol = dsm->GetGlobalVol();
  131.                             
  132.                             dsm->SetGlobalVol((mastervol>=5) ? mastervol-5 : 0);
  133.                             break;
  134.                         }
  135.                         case '+':
  136.                         {
  137.                             uword mastervol = dsm->GetGlobalVol();
  138.                             
  139.                             dsm->SetGlobalVol((mastervol<=250) ? mastervol+5 : 255);
  140.                             break;
  141.                         }
  142.                         case '[':
  143.                             dsm->GoToPrevPattern();
  144.                             break;
  145.                         case ']':
  146.                             dsm->GoToNextPattern();
  147.                             break;
  148.                         case ' ':
  149.                             dsm->TogglePause();
  150.                             break;
  151.                         default:
  152.                             short key = (theEvent.message & charCodeMask) - 'A';
  153.                             
  154.                             if ((key >= 0) && (key < dsm->GetMixChannels()-1)) 
  155.                             {
  156.                                 dsm->SetFreq(0, 16316);
  157.                                 dsm->SetBalance(0, 128);
  158.                                 dsm->SetVolume(0, 64);
  159.                                 dsm->VoicePlay(0, 0, key);
  160.                             }
  161.                     }
  162.                 }
  163.             }
  164.     }
  165.     
  166.     dsm->PlayStop(true);
  167.  
  168.     delete song;
  169.     delete dsm;
  170.  
  171.     FlushEvents(everyEvent, 0);
  172. }
  173.